package com.studytime;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphUser;

public class MainActivity extends FragmentActivity {
	
	private MainFragment mainFragment;
	private static final String TAG = "MainFragment";
	 private Handler handler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);

	    if (savedInstanceState == null) {
	        // Add the fragment on initial activity setup
	        mainFragment = new MainFragment();
	        getSupportFragmentManager()
	        .beginTransaction()
	        .add(android.R.id.content, mainFragment)
	        .commit();
	    } else {
	        // Or set the fragment from restored state info
	        mainFragment = (MainFragment) getSupportFragmentManager()
	        .findFragmentById(android.R.id.content);
	    }
	    handler.postDelayed(new Runnable() {
            @Override
            public void run() {            	
            	isLoggedIn();          
            }
        }, 200);
    }
	public static String uname;
	
	////This function will check if user is login. If yes it will redirect user to measure activity
	public void isLoggedIn() {
		Session session = Session.getActiveSession();
		if (session != null && session.isOpened()) {
			Request.executeMeRequestAsync(Session.getActiveSession(),
					new Request.GraphUserCallback() {
						@Override
						public void onCompleted(GraphUser user,
								Response response) {
							// TODO Auto-generated method stub
							if (user != null) {
								System.out.println("user id1 = " + user.getName());
								uname = user.getName();
								Toast.makeText(MainActivity.this, "Hello " + uname,Toast.LENGTH_SHORT).show();
								Intent intent = new Intent(MainActivity.this,MeasureActivity.class);
								startActivity(intent);
							}
						}
					});
			System.out.println("Logged in");
		} else {
			Toast.makeText(MainActivity.this, "Not Login", Toast.LENGTH_SHORT)
			.show();
		}
	} 
}
